package cn.com.blueInfo.HaiYuan.business.drugs.service.impl;

import cn.com.blueInfo.HaiYuan.business.drugs.entity.DrugsEffect;
import cn.com.blueInfo.HaiYuan.business.drugs.mapper.DrugsEffectMapper;
import cn.com.blueInfo.HaiYuan.business.drugs.service.DrugsEffectService;
import cn.com.blueInfo.utils.result.BootstrapTable;
import cn.com.blueInfo.utils.result.ResultInfo;
import cn.com.blueInfo.utils.util.JSONUtil;
import cn.com.blueInfo.utils.util.RequestPayload;
import cn.com.blueInfo.utils.util.UUIDUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 药品功效功能信息服务接口具体实现
 * @ClassName: DrugsEffectServiceImpl
 * @author suxch
 * @date 2019年5月28日  下午9:53:37
 */
@Log4j2
@Service
public class DrugsEffectServiceImpl implements DrugsEffectService {

    @Autowired
    private DrugsEffectMapper effectMapper;

    /**
     * 分页查询药品功能功效数据具体实现
     * @Title: queryDrugsEffectDataByPage
     * @param request
     * @return BootstrapTable
     */
    @Override
    public BootstrapTable queryDrugsEffectDataByPage(HttpServletRequest request) {
        BootstrapTable result = new BootstrapTable();
        JSONObject param = RequestPayload.analysis(request);
        log.info("bootstrapTable请求参数" + param);

        Map<String, Object> paramMap = JSON.parseObject(param.toJSONString());

        try {
            Integer count = effectMapper.queryDrugsEffectInfoCount(paramMap);
            List<DrugsEffect> drugsBaseList = effectMapper.queryDrugsEffectInfo(paramMap);
            result.setTotal(count);
            result.setRows(drugsBaseList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result.setTotal(0);
            result.setRows(null);
        }
        return result;
    }

    /**
     * 查询最大排序编号具体实现
     * @Title: queryMaxOrderNumber
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo queryMaxOrderNumber(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        try {
            Integer maxNum = effectMapper.queryMaxOrderNumber();
            if (maxNum == null) maxNum = 0;
            maxNum += 1;
            result.setData(maxNum);
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("最大编号获取失败，请联系管理员");
            result.setError(e.getMessage());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 新增药品功能功效数据具体实现
     * @Title: addDrugsEffectData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo addDrugsEffectData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            DrugsEffect drugsEffect = JSON.parseObject(requestData, DrugsEffect.class);
            drugsEffect.setUnid(UUIDUtil.createUUID());

            Map<String, Object> queryParam = new HashMap<String, Object>();
            queryParam.put("domainCode", drugsEffect.getDomainCode());
            queryParam.put("baseId", drugsEffect.getBaseId());

            try {
                Integer isExist = effectMapper.queryDrugsEffectInfoCount(queryParam);
                if (isExist == 1) {
                    result.setResult(false);
                    result.setMessage("该药品功能功效信息已存在，请修改");
                } else {
                    int add = effectMapper.addDrugsEffectInfo(drugsEffect);
                    if (add == 1) {
                        result.setResult(true);
                        result.setMessage("药品功能功效信息添加成功");
                    }
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setError(e.getMessage());
                result.setMessage("药品功能功效信息增加失败，请联系管理员");
                log.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常，请联系管理员");
        }
        return result;
    }

    /**
     * 更新药品功能功效数据具体实现
     * @Title: updateDrugsEffectData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo updateDrugsEffectData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String requestData = request.getParameter("requestParam");

        if (JSONUtil.isToJSONObject(requestData)) {
            DrugsEffect drugsEffect = JSON.parseObject(requestData, DrugsEffect.class);

            try {
                int update = effectMapper.updateDrugsEffectInfoByUnid(drugsEffect);
                if (update == 1) {
                    result.setResult(true);
                    result.setMessage("药品功能功效信息更新成功");
                }
            } catch (Exception e) {
                result.setResult(false);
                result.setMessage("药品功能功效信息更新失败，请联系管理员");
                result.setError(e.getMessage());
                log.error(e.getMessage());
                e.printStackTrace();
            }
        } else {
            result.setResult(false);
            result.setMessage("参数异常");
        }
        return result;
    }

    /**
     * 删除药品功能功效数据具体实现
     * @Title: deleteDrugsEffectData
     * @param request
     * @return ResultInfo
     */
    @Override
    public ResultInfo deleteDrugsEffectData(HttpServletRequest request) {
        ResultInfo result = new ResultInfo();
        String drugsUnid = request.getParameter("unid");

        try {
            int update = effectMapper.delDrugsEffectInfoByUnid(drugsUnid);
            if (update == 1) {
                result.setResult(true);
                result.setMessage("药品功能功效信息删除成功");
            }
        } catch (Exception e) {
            result.setResult(false);
            result.setMessage("药品功能功效信息删除失败，请联系管理员");
            result.setError(e.getMessage());
            log.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

}
